package com.ztesoft.web.baseconfig.db.dao;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.ztesoft.core.common.Page;
import com.ztesoft.framework.exception.SysRuntimeException;
import com.ztesoft.framework.util.StringUtils;
import com.ztesoft.framework.util.Utils;
import com.ztesoft.web.baseconfig.db.arg.DmPlanArg;
import com.ztesoft.web.baseconfig.db.arg.DmPlanArg.DmPlanCriteria;
import com.ztesoft.web.baseconfig.db.dao.mapper.IDmPlanMapper;
import com.ztesoft.web.baseconfig.db.po.DmPlanPO;

@Repository
public class DmPlanDao extends SqlSessionDaoSupport {

	@Resource(name = "majorSqlSessionTemplate")
	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		super.setSqlSessionTemplate(sqlSessionTemplate);
	}

	public List<Integer> countByArg(DmPlanArg arg) {
		return getMapper().countByArg(arg);
	}

	public int deleteByArg(DmPlanArg arg) {
		return getMapper().deleteByArg(arg);
	}

	public List<DmPlanPO> selectByArg(DmPlanArg arg) {
		return getMapper().selectByArg(arg);
	}

	public int updateByArgSelective(DmPlanPO record, DmPlanArg arg) {
		return getMapper().updateByArgSelective(record, arg);
	}

	public int updateByArg(DmPlanPO record, DmPlanArg arg) {
		return getMapper().updateByArg(record, arg);
	}

	public Page<DmPlanPO> selectByArgAndPage(DmPlanArg arg, Page<DmPlanPO> resultPage) {
		List<DmPlanPO> resultList = getMapper().selectByArgAndPage(arg, resultPage);

		resultPage.setResultList(resultList);
		return resultPage;
	}
	
	public Page<DmPlanPO> selectByArgAndPageForFile(DmPlanPO record, Page<DmPlanPO> resultPage) {
		List<DmPlanPO> resultList = getMapper().selectByArgAndPageForFile(record, resultPage);
		resultPage.setResultList(resultList);
		return resultPage;
	}

	// 草稿箱查询
	public Page<DmPlanPO> selectByArgAndPageDraft(DmPlanArg arg, Page<DmPlanPO> resultPage) {
		List<DmPlanPO> resultList = getMapper().selectByArgAndPageDraft(arg, resultPage);
		int listSize = resultList.size();
		for (int i = 0; i < listSize; i++) {
			// 判断源数据是否为空，为空显示为——
			if (Utils.isEmpty(resultList.get(i).getSourceData())) {
				resultList.get(i).getSourceData().add("-- --");
			}
			// 判断目标数据是否为空，为空显示为——
			if (Utils.isEmpty(resultList.get(i).getTargetData())) {
				resultList.get(i).getTargetData().add("-- --");
			}
		}
		resultPage.setResultList(resultList);
		return resultPage;
	}

	// 计划维护查询
	public Page<DmPlanPO> selectByArgAndPageMaint(DmPlanArg arg, Page<DmPlanPO> resultPage) {
		List<DmPlanPO> resultList = getMapper().selectByArgAndPageMaint(arg, resultPage);
		int listSize = resultList.size();
		for (int i = 0; i < listSize; i++) {
			// 判断源数据是否为空，为空显示为——
			if (Utils.isEmpty(resultList.get(i).getSourceData())) {
				resultList.get(i).getSourceData().add("-- --");
			}
			// 判断目标数据是否为空，为空显示为——
			if (Utils.isEmpty(resultList.get(i).getTargetData())) {
				resultList.get(i).getTargetData().add("-- --");
			}
		}
		resultPage.setResultList(resultList);
		return resultPage;
	}

	public int insert(DmPlanPO record) {
		return getMapper().insert(record);
	}

	public int insertSelective(DmPlanPO record) {
		return getMapper().insertSelective(record);
	}

	public int insertBatch(List<DmPlanPO> records) {
		return getMapper().insertBatch(records);
	}

	// 删除草稿箱，把状态改为00X
	public int deleteByPrimaryKey(Integer key) {
		return getMapper().deleteByPrimaryKey(key);
	}

	// 删除计划维护，把状态改为00X
	public int deleteMaint(Integer key) {
		return getMapper().deleteMaint(key);
	}

	public DmPlanPO selectByPrimaryKey(Integer key) {
		return getMapper().selectByPrimaryKey(key);
	}
	
	public DmPlanPO selectByDmStrategyId(Integer key) {
		return getMapper().selectByDmStrategyId(key);
	}

	/**
	 * 根据表名，查找计划
	 * 
	 * @param tableName
	 * @return
	 */
	public DmPlanPO selectByTableName(String tableName) {
		return getMapper().selectByTableName(tableName);
	}

	/**
	 * 查询是否存在相同名称的计划
	 * 
	 * @param planName
	 * @return
	 */
	public int selectByPlanName(String planName) {
		return getMapper().selectByPlanName(planName);
	}

	// 判断该NodeId是否已经被创建
	public int selectByNodeId(Integer moduleId) {
		return getMapper().selectByNodeId(moduleId);
	}

	public int updateByPrimaryKeySelective(DmPlanPO record) {
		return getMapper().updateByPrimaryKeySelective(record);
	}
	public int upexpDate(DmPlanPO record) {
		return getMapper().upexpDate(record);
	}

	public int updateByPrimaryKey(DmPlanPO record) {
		return getMapper().updateByPrimaryKey(record);
	}

	public List<DmPlanPO> selectByDmPlanAndGroupNbr() {
		return getMapper().selectByDmPlanAndGroupNbr();
	}

	/**
	 * 根据传入的Map条件进行查询，当前仅支持所有Map中Key字段的EqualTo查询
	 * 
	 * @param params
	 *            Map,Key=字段名，value=查询值
	 * @return
	 */
	public List<DmPlanPO> selectByMap(Map<String, Object> params) {
		return (selectByArg(buildQueryObject(params)));
	}

	private DmPlanArg buildQueryObject(Map<String, Object> params) {

		DmPlanArg arg = new DmPlanArg();
		DmPlanCriteria criteria = arg.createCriteria();

		Class criteriaClass = criteria.getClass();
		Set keys = params.keySet();

		if (keys != null) {

			Iterator iterator = keys.iterator();

			while (iterator.hasNext()) {

				Object key = iterator.next();
				Object value = params.get(key);
				for (Method method : criteriaClass.getMethods()) {
					if (method.getName().equals("and" + StringUtils.toUpperCaseFirstOne(key.toString()) + "EqualTo")) {
						try {
							method.invoke(criteria, value);
						} catch (Exception e) {
							throw new SysRuntimeException(e);
						}
						break;
					}
				}
			}
		}
		return arg;
	}

	public IDmPlanMapper getMapper() {
		return getSqlSession().getMapper(IDmPlanMapper.class);
	}

	public DmPlanPO selectByExecType(String execType) {
		DmPlanPO po = getMapper().selectByExecType(execType);
		return po;
	}

	public List<DmPlanPO> selectSourceByDmPlanId(Integer dmPlanId) {
		return getMapper().selectSourceByDmPlanId(dmPlanId);
	}

	public DmPlanPO selectOperTypeByDmPlanId(Integer dmPlanId) {
		return getMapper().selectOperTypeByDmPlanId(dmPlanId);
	}

}
